﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SLogo.Helper;

namespace SLogo.Compile
{
    /// <summary>
    /// 语法分析、语义分析和中间代码生成
    /// </summary>
    class SyntaxAndSemanticAnalysis
    {
        /// <summary>
        /// 中间代码表
        /// </summary>
        public static List<IntermediateCode> IntermediateCodeList { get; set; }
        /// <summary>
        /// 自定义过程表
        /// </summary>
        public static List<CustomProcess> CustomProcessList { get; set; }
        /// <summary>
        /// 语法分析是否成功
        /// </summary>
        public static bool Success { get; private set; }
        //系统内置命令的名称列表
        private static readonly String[] CommandNames = {
                                                            "pu", "pd", "cs", "home", "fd", "bk", "rt", "lt", "setx",
                                                            "sety", "setxy", "seth", "circle", "circlefill", "ellipse",
                                                            "ellipsefill", "rect", "rectfill", "poly", "polyfill",
                                                            "setfrcolor", "setbkcolor", "to", "if", "ifelse",
                                                            "repeat", "make", "print", "end", "stop", "include"
                                                        };
        //中间代码当前的条数
        private static int _numChain = 0;
        //自定义过程的代码块的当前条数
        private static int _cusProNumChain = 0;

        /// <summary>
        /// 静态初始化
        /// </summary>
        static SyntaxAndSemanticAnalysis()
        {
            Success = true;
            IntermediateCodeList = new List<IntermediateCode>();
            CustomProcessList = new List<CustomProcess>();
        }

        /// <summary>
        /// 程序 A
        /// </summary>
        /// <returns></returns>
        public static void Program()
        {
            LexicalAnalysis.Scanner(); //读入单词
            SentenceSequence(); //进入语句串分析
        }

        /// <summary>
        /// 语句串 B
        /// </summary>
        /// <returns></returns>
        static void SentenceSequence()
        {
            while (LexicalAnalysis.Syn == 10) //换行
            {
                LexicalAnalysis.Scanner(); //读入单词
            }
            while (LexicalAnalysis.Syn == 3) //引用文件
            {
                ReferenceFiles(); //进入引用文件分析
            }
            while (LexicalAnalysis.Syn == 10) //换行
            {
                LexicalAnalysis.Scanner();
            }
            while (LexicalAnalysis.Syn == 132 || LexicalAnalysis.Syn == 1)
            {
                Sentence(); //进入语句分析
                while (LexicalAnalysis.Syn == 10)
                {
                    LexicalAnalysis.Scanner();
                }
            }
            while (LexicalAnalysis.Syn == 10)
            {
                LexicalAnalysis.Scanner();
            }
            if (LexicalAnalysis.Syn != 0)
            {
                if (LexicalAnalysis.Syn == 10)
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：错误的命令，或者非法使用回车\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line - 1,
                                                       LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                }
                else
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：错误的命令，或者非法使用回车\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                       LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
        }

        /// <summary>
        /// 语句 C
        /// </summary>
        /// <returns></returns>
        static void Sentence()
        {
            if (LexicalAnalysis.Syn == 1)
            {
                ProcessDefine(); //进入过程语句分析
            }
            else if (LexicalAnalysis.Syn == 132)
            {
                BasicSentence(null); //进入基本语句分析
            }
        }

        /// <summary>
        /// 基本语句 D
        /// </summary>
        /// <returns></returns>
        static void BasicSentence(CustomProcess customProcess)
        {
            if (LexicalAnalysis.Token.Equals("repeat"))
            {
                Repeate(customProcess); //重复结构
            }
            else if (LexicalAnalysis.Token.Equals("if") ||LexicalAnalysis.Token.Equals("ifelse"))
            {
                Branch(customProcess); //分支结构
            }
            else
            {
                Command(customProcess); //命令语句
            }
        }

        /// <summary>
        /// 过程定义 E
        /// </summary>
        /// <returns></returns>
        static void ProcessDefine()
        {
            IntermediateCodeList.Add(new IntermediateCode("to", null, null, LexicalAnalysis.FilenameStack.Peek(),
                                                          LexicalAnalysis.Line));
            _numChain++;
            LexicalAnalysis.Scanner();
            CustomProcess customProcess = new CustomProcess();
            if (LexicalAnalysis.Syn != 132) //若不为标识符
            {
                //一些错误处理
                ConsoleHelper.StdErr(String.Format("语法错误：过程名错误，不符合标识符的规范\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
            }
            else if (CommandNames.Contains(LexicalAnalysis.Token)) //若是关键字
            {
                ConsoleHelper.StdErr(String.Format("语法错误：过程名不能为关键字\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
            }
            else if(CustomProcessList.FirstOrDefault(c => c.Name == LexicalAnalysis.Token) != null)
            {
                ConsoleHelper.StdErr(String.Format("语法错误：过程不能重复定义\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
            }
            else
            {
                customProcess.Name = LexicalAnalysis.Token;
            }
            LexicalAnalysis.Scanner();
            while (LexicalAnalysis.Syn == 139) //是否为引用变量标志“:”
            {
                Variable v = new Variable(VariRef()); //变量引用
                if (customProcess.Arguments.FirstOrDefault(a => a.Name == v.Name) == null)
                {
                    customProcess.Arguments.Add(v);
                }
                else
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：过程的参数名不能重复\n\t错误位置：{0} line {1}",
                                         LexicalAnalysis.FilenameStack.Peek(), LexicalAnalysis.Line));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
            if (LexicalAnalysis.Syn == 10) //换行
            {
                LexicalAnalysis.Scanner();
                while (LexicalAnalysis.Syn == 10)
                {
                    LexicalAnalysis.Scanner();
                }
            }
            else
            {
                ConsoleHelper.StdErr(String.Format("语法错误：to不是单独一行\n\t错误位置：{1} line {0}\n",
                                                   LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
                LexicalAnalysis.Scanner();
            }
            if (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to")) //不是关键字的标识符
            {
                int sign = 0; //是否存在换行的标志
                while (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to"))
                {
                    BasicSentence(customProcess); //基本语句分析
                    sign = 0;
                    while (LexicalAnalysis.Syn == 10) //换行
                    {
                        LexicalAnalysis.Scanner();
                        sign = 1;
                    }
                }
                if (sign == 1 && LexicalAnalysis.Syn == 2) //存在换行且有结束符end
                {
                    CustomProcessList.Add(customProcess);
                    _cusProNumChain = 0;
                    LexicalAnalysis.Scanner();
                    if (LexicalAnalysis.Syn == 10 ||
                        LexicalAnalysis.Pointer >= LexicalAnalysis.SourceCode.Peek().Length)
                    {
                        while (LexicalAnalysis.Syn == 10)
                        {
                            LexicalAnalysis.Scanner();
                        }
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：end不是单独一行\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else //一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：end不是单独一行\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                }
                else
                {
                    if (LexicalAnalysis.Syn == 10)
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：缺少end或者end不是单独一行\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;

                    }
                    else //一些错误处理
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：缺少end或者end不是单独一行\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                        LexicalAnalysis.Scanner();
                    }
                }
            }
            else if(LexicalAnalysis.Token.Equals("to"))
            {
                ConsoleHelper.StdErr(String.Format("语法错误：过程不能嵌套定义\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
                LexicalAnalysis.Scanner();
            }
        }

        /// <summary>
        /// 命令 F
        /// </summary>
        /// <returns></returns>
        static void Command(CustomProcess customProcess)
        {
            String op = LexicalAnalysis.Token;
            int line = LexicalAnalysis.Line;
            List<Agument> args = new List<Agument>();
            LexicalAnalysis.Scanner();
            if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                LexicalAnalysis.Syn == 40) //命令接后的参数
            {
                while (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                       LexicalAnalysis.Syn == 40)
                {
                    Variable para;
                    Parameter(customProcess, out para);
                    if (para != null && args != null) //如果没有错误
                    {
                        args.Add(new Agument(ref para));
                    }
                    else
                    {
                        args = null;
                    }
                }
            }
            if (args != null)
            {
                IntermediateCode iCode = new IntermediateCode(op, args, null,
                                                              LexicalAnalysis.FilenameStack.Peek(),
                                                              line);
                if (customProcess == null)
                {
                    IntermediateCodeList.Add(iCode);
                    _numChain++;
                }
                else
                {
                    customProcess.CodeBlock.Add(iCode);
                    _cusProNumChain++;
                }
            }
        }

        /// <summary>
        ///  重复 G
        /// </summary>
        /// <returns></returns>
        static void Repeate(CustomProcess customProcess)
        {
            LexicalAnalysis.Scanner();
            if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 40) //重复命令后接的参数
            {
                Variable num; //数值表达式的Place
                NumExp(customProcess, out num); //数值表达式分析
                Variable n = new Variable(NewTemp());
                if (num != null) //检查是否为正整数
                {
                    IntermediateCode iCodeLoopVar = new IntermediateCode("#=", new List<Agument> {new Agument(ref num)},
                                                                         n, LexicalAnalysis.FilenameStack.Peek(),
                                                                         LexicalAnalysis.Line);
                    IntermediateCode iCode = new IntermediateCode("#check", new List<Agument> {new Agument(ref num)},
                                                                  null,
                                                                  LexicalAnalysis.FilenameStack.Peek(),
                                                                  LexicalAnalysis.Line);
                    if (customProcess == null) //如果是在自定义过程之外，则将中间代码添加到中间代码表中
                    {
                        IntermediateCodeList.Add(iCode);
                        _numChain++;
                        IntermediateCodeList.Add(iCodeLoopVar);
                        _numChain++;
                    }
                    else //否则添加到过程表中
                    {
                        customProcess.CodeBlock.Add(iCode);
                        _cusProNumChain++;
                        customProcess.CodeBlock.Add(iCodeLoopVar);
                        _cusProNumChain++;
                    }
                }
                int bpChain = customProcess == null ? _numChain + 2 : _cusProNumChain + 2;
                Variable condition = new Variable(NewTemp()); //循环条件
                if (num != null) //如果没有发生错误
                {
                    List<Agument> args = new List<Agument> { new Agument(ref n), new Agument(0) };
                    IntermediateCode iCodeCondition = new IntermediateCode(">", args, condition,
                                                                           LexicalAnalysis.FilenameStack.Peek(),
                                                                           LexicalAnalysis.Line);
                    args = new List<Agument> {new Agument(ref condition),
                                   new Agument(customProcess == null ? _numChain + 3 : _cusProNumChain + 3)
                               }; //if命令
                    IntermediateCode iCodeIf = new IntermediateCode("if", args, null,
                                                                    LexicalAnalysis.FilenameStack.Peek(),
                                                                    LexicalAnalysis.Line);
                    //goto命令
                    IntermediateCode iCodeGoto = new IntermediateCode("#goto", new List<Agument>(), null,
                                                                      LexicalAnalysis.FilenameStack.Peek(),
                                                                      LexicalAnalysis.Line);
                    if (customProcess == null) //如果是在自定义过程之外，则将中间代码添加到中间代码表中
                    {
                        IntermediateCodeList.Add(iCodeCondition);
                        _numChain++;
                        IntermediateCodeList.Add(iCodeIf);
                        _numChain++;
                        IntermediateCodeList.Add(iCodeGoto);
                        _numChain++;
                    }
                    else //否则添加到过程表中
                    {
                        customProcess.CodeBlock.Add(iCodeCondition);
                        _cusProNumChain++;
                        customProcess.CodeBlock.Add(iCodeIf);
                        _cusProNumChain++;
                        customProcess.CodeBlock.Add(iCodeGoto);
                        _cusProNumChain++;
                    }
                }
                if (LexicalAnalysis.Syn == 91) //命令序列开始标志[
                {
                    LexicalAnalysis.Scanner();
                    if (LexicalAnalysis.Token.Equals("to"))
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：\"to\"只能在顶端\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else //一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：\"to\"只能在顶端\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));

                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                    while (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to")) //当前的标识符非关键字
                    {
                        BasicSentence(customProcess); //基本语句分析
                    }
                    if (LexicalAnalysis.Syn == 93) //命令序列结束标志]
                    {
                        LexicalAnalysis.Scanner();
                        if (n != null)
                        {
                            List<Agument> args = new List<Agument> {new Agument(ref n), new Agument(1)};
                            IntermediateCode iCodeN = new IntermediateCode("-", args, n,
                                                                           LexicalAnalysis.FilenameStack.Peek(),
                                                                           LexicalAnalysis.Line);
                            args = new List<Agument>();
                            args.Add(new Agument(ref n)); //循环条件
                            args.Add(new Agument(0));
                            IntermediateCode iCodeCondition = new IntermediateCode(">", args, condition,
                                                                                   LexicalAnalysis.FilenameStack.
                                                                                       Peek(),
                                                                                   LexicalAnalysis.Line);
                            args = new List<Agument>();
                            args.Add(new Agument(bpChain - 1)); //循环
                            IntermediateCode iCodeGoto = new IntermediateCode("#goto", args, null,
                                                                              LexicalAnalysis.FilenameStack.Peek(),
                                                                              LexicalAnalysis.Line);
                            if (customProcess == null) //如果是在自定义过程之外，则将中间代码添加到中间代码表中
                            {
                                IntermediateCodeList.Add(iCodeN);
                                _numChain++;
                                IntermediateCodeList.Add(iCodeCondition);
                                _numChain++;
                                IntermediateCodeList.Add(iCodeGoto);
                                _numChain++;
                            }
                            else //否则添加到过程表中
                            {
                                customProcess.CodeBlock.Add(iCodeN);
                                _cusProNumChain++;
                                customProcess.CodeBlock.Add(iCodeCondition);
                                _cusProNumChain++;
                                customProcess.CodeBlock.Add(iCodeGoto);
                                _cusProNumChain++;
                            }
                            //回填
                            BackPatch(bpChain, customProcess == null ? _numChain : _cusProNumChain, customProcess);
                        }
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有结束的\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else //一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有结束的\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                }
                else
                {
                    if (LexicalAnalysis.Syn == 10)
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：没有开始的\"[\"!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    else //一些错误处理
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：没有开始的\"[\"!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                        LexicalAnalysis.Scanner();
                    }
                }
            }
        }

        /// <summary>
        /// 分支 H
        /// </summary>
        /// <returns></returns>
        static void Branch(CustomProcess customProcess)
        {
            if (LexicalAnalysis.Token.Equals("if")) //if分支
            {
                LexicalAnalysis.Scanner();
                if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                    LexicalAnalysis.Syn == 40 || LexicalAnalysis.Syn == 140)
                {
                    Variable cond;
                    CondExp(customProcess, out cond); //关系表达式
                    int bpChain = customProcess == null ? _numChain + 1 : _cusProNumChain + 1;
                    if(cond != null)//如果没有错误，添加if语句
                    {
                        List<Agument> args = new List<Agument> {new Agument(ref cond), new Agument(bpChain + 1)};
                        IntermediateCode iCodeIf = new IntermediateCode("if", args, null,
                                                                        LexicalAnalysis.FilenameStack.Peek(),
                                                                        LexicalAnalysis.Line);
                        IntermediateCode iCodeGoto = new IntermediateCode("#goto", new List<Agument>(), null,
                                                                          LexicalAnalysis.FilenameStack.Peek(),
                                                                          LexicalAnalysis.Line);
                        if (customProcess == null)
                        {
                            IntermediateCodeList.Add(iCodeIf);
                            _numChain++;
                            IntermediateCodeList.Add(iCodeGoto);
                            _numChain++;
                        }
                        else
                        {
                            customProcess.CodeBlock.Add(iCodeIf);
                            _cusProNumChain++;
                            customProcess.CodeBlock.Add(iCodeGoto);
                            _cusProNumChain++;
                        }
                    }
                    if (LexicalAnalysis.Syn == 91) //开始中括号
                    {
                        LexicalAnalysis.Scanner();
                        while (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to")) //当前的标识符非关键字
                        {
                            BasicSentence(customProcess); //基本语句分析
                        }
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            //一些错误处理
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                              LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                              LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                    if (LexicalAnalysis.Syn == 93) //结束中括号
                    {
                        if (cond != null)
                        {
                            BackPatch(bpChain, customProcess == null ? _numChain : _cusProNumChain, customProcess); //回填
                        }
                        LexicalAnalysis.Scanner();
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                              LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                              LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                }
                else
                {
                    if (LexicalAnalysis.Syn == 10)
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：关系表达式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    else//一些错误处理
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：关系表达式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                        LexicalAnalysis.Scanner();
                    }
                }
            }
            else if (LexicalAnalysis.Token.Equals("ifelse")) //ifelse分支
            {
                LexicalAnalysis.Scanner();
                if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                    LexicalAnalysis.Syn == 40 || LexicalAnalysis.Syn == 140)
                {
                    Variable cond;
                    CondExp(customProcess, out cond);
                    int bpChain = customProcess == null ? _numChain + 1 : _cusProNumChain + 1;
                    if (cond != null)//如果没有错误，添加if语句
                    {
                        List<Agument> args = new List<Agument> { new Agument(ref cond), new Agument(bpChain + 1) };
                        IntermediateCode iCodeIf = new IntermediateCode("if", args, null,
                                                                        LexicalAnalysis.FilenameStack.Peek(),
                                                                        LexicalAnalysis.Line);
                        IntermediateCode iCodeGoto = new IntermediateCode("#goto", new List<Agument>(), null,
                                                                          LexicalAnalysis.FilenameStack.Peek(),
                                                                          LexicalAnalysis.Line);
                        if (customProcess == null)
                        {
                            IntermediateCodeList.Add(iCodeIf);
                            _numChain++;
                            IntermediateCodeList.Add(iCodeGoto);
                            _numChain++;
                        }
                        else
                        {
                            customProcess.CodeBlock.Add(iCodeIf);
                            _cusProNumChain++;
                            customProcess.CodeBlock.Add(iCodeGoto);
                            _cusProNumChain++;
                        }
                    }
                    if (LexicalAnalysis.Syn == 91) //开始中括号
                    {
                        LexicalAnalysis.Scanner();
                        while (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to")) //当前的标识符非关键字
                        {
                            BasicSentence(customProcess); //基本语句分析
                        }
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                             LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                    int bpChainAfterElse = customProcess == null ? _numChain : _cusProNumChain;
                    if (LexicalAnalysis.Syn == 93) //结束中括号
                    {
                        if(cond != null)
                        {
                            //if结束之后的goto
                            IntermediateCode iCodeGoto = new IntermediateCode("#goto", new List<Agument>(), null,
                                                                               LexicalAnalysis.FilenameStack.Peek(),
                                                                               LexicalAnalysis.Line);
                            if(customProcess == null)
                            {
                                IntermediateCodeList.Add(iCodeGoto);
                                _numChain++;
                            }
                            else
                            {
                                customProcess.CodeBlock.Add(iCodeGoto);
                                _cusProNumChain++;
                            }
                            //回填
                            BackPatch(bpChain, customProcess == null ? _numChain : _cusProNumChain, customProcess);
                        }
                        LexicalAnalysis.Scanner();
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                    if (LexicalAnalysis.Syn == 91) //开始中括号
                    {
                        LexicalAnalysis.Scanner();
                        while (LexicalAnalysis.Syn == 132 && !LexicalAnalysis.Token.Equals("to")) //当前的标识符非关键字
                        {
                            BasicSentence(customProcess); //基本语句分析
                        }
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"[\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                    if (LexicalAnalysis.Syn == 93) //结束中括号
                    {
                        if(cond != null)
                        {
                            BackPatch(bpChainAfterElse, customProcess == null ? _numChain : _cusProNumChain,
                                      customProcess);//回填
                        }
                        LexicalAnalysis.Scanner();
                    }
                    else
                    {
                        if (LexicalAnalysis.Syn == 10)
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line - 1,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                        else//一些错误处理
                        {
                            ConsoleHelper.StdErr(String.Format("语法错误：没有\"]\"!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                              LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                            LexicalAnalysis.Scanner();
                        }
                    }
                }
                else
                {
                    if (LexicalAnalysis.Syn == 10)
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：关系表达式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    else//一些错误处理
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：关系表达式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                        LexicalAnalysis.Scanner();
                    }
                }
            }
        }

        /// <summary>
        /// 参数 I
        /// </summary>
        /// <returns></returns>
        static void Parameter(CustomProcess customProcess, out Variable para)
        {
            para = null;
            if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                LexicalAnalysis.Syn == 40)
            {
                if (LexicalAnalysis.Syn == 130) //浮点数字
                {
                    NumExp(customProcess, out para); //数值表达式分析
                }
                else if (LexicalAnalysis.Syn == 139) //引用变量
                {
                    NumExp(customProcess, out para);
                }
                else if (LexicalAnalysis.Syn == 131) //字符串
                {
                    para = new Variable(NewTemp(), LexicalAnalysis.Token);
                    LexicalAnalysis.Scanner();
                }
                else if (LexicalAnalysis.Syn == 40) //小括号
                {
                    NumExp(customProcess, out para);
                }
            }
        }

        /// <summary>
        /// 数值表达式 J
        /// </summary>
        /// <returns></returns>
        static void NumExp(CustomProcess customProcess, out Variable num)
        {
            num = null;
            Variable i;//项的Place
            Item(customProcess, out i);
            Variable m;//数值中间项的Place
            String op;//运算符
            NumMidItem(customProcess, out op, out m);
            if (i != null)//如果没有发生错误
            {
                if (op != "" && m != null)//如果有后边的项
                {
                    num = new Variable(NewTemp());
                    List<Agument> args = new List<Agument> {new Agument(ref i), new Agument(ref m)};//参数列表
                    IntermediateCode iCode = new IntermediateCode(op, args, num, LexicalAnalysis.FilenameStack.Peek(),
                                                 LexicalAnalysis.Line);//中间代码
                    if (customProcess == null)//填入中间代码表
                    {
                        IntermediateCodeList.Add(iCode);
                        _numChain++;
                    }
                    else//填入自定义过程
                    {
                        customProcess.CodeBlock.Add(iCode);
                        _cusProNumChain++;
                    }
                }
                else
                {
                    num = i;
                }
            }
        }

        /// <summary>
        /// 数值中间项 j
        /// </summary>
        /// <returns></returns>
        static void  NumMidItem(CustomProcess customProcess, out String op, out Variable m)
        {
            op = "";
            m = null;
            if (LexicalAnalysis.Syn == 43 || LexicalAnalysis.Syn == 45)
            {
                op = LexicalAnalysis.Syn == 43 ? "+" : "-";
                LexicalAnalysis.Scanner();
                Item(customProcess, out m); //项分析
                while (LexicalAnalysis.Syn == 43 || LexicalAnalysis.Syn == 45)
                {
                    Variable i;
                    String o = LexicalAnalysis.Syn == 43 ? "+" : "-";
                    LexicalAnalysis.Scanner();
                    Item(customProcess, out i); //项分析
                    if (m != null && i != null)
                    {
                        Variable res = new Variable(NewTemp());
                        List<Agument> args = new List<Agument> { new Agument(ref m), new Agument(ref i) };
                        IntermediateCode iCode = new IntermediateCode(o, args, res, LexicalAnalysis.FilenameStack.Peek(),
                                                                      LexicalAnalysis.Line);
                        if (customProcess == null)
                        {
                            IntermediateCodeList.Add(iCode);
                            _numChain++;
                        }
                        else
                        {
                            customProcess.CodeBlock.Add(iCode);
                            _cusProNumChain++;
                        }
                        m = res;
                    }
                }
            }
            else if (LexicalAnalysis.Token != "}" && LexicalAnalysis.Token != ")" && LexicalAnalysis.Token != "&" &&
                     LexicalAnalysis.Token != "|" && LexicalAnalysis.Token != ">" && LexicalAnalysis.Token != ">=" &&
                     LexicalAnalysis.Token != "<" && LexicalAnalysis.Token != "<=" && LexicalAnalysis.Token != "=" &&
                     LexicalAnalysis.Token != "!=" && LexicalAnalysis.Token != "\n" && LexicalAnalysis.Token != "[" &&
                     LexicalAnalysis.Token != "]" && LexicalAnalysis.Syn != 132 && LexicalAnalysis.Syn != 130 &&
                     LexicalAnalysis.Syn != 131 && LexicalAnalysis.Syn != 139 &&LexicalAnalysis.Syn != 40&&
                     LexicalAnalysis.Pointer < LexicalAnalysis.SourceCode.Peek().Length) //若不为k的Follow集
            {
                if (LexicalAnalysis.Syn == 10)
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line - 1,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                }
                else//一些错误处理
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
        }

        /// <summary>
        /// 项 K
        /// </summary>
        /// <returns></returns>
        static void Item(CustomProcess customProcess, out Variable item)
        {
            item = null;
            Variable f;//存放因子的Place
            Factor(customProcess, out f); //因子
            Variable m;//存放中间项的Place
            String op;//存放运算符
            ItemMidEle(customProcess, out op, out m); //中间项
            if(f != null)//如果没有发生错误
            {
                if (op != "" && m != null)//如果有后边的项
                {
                    item = new Variable(NewTemp());
                    List<Agument> args = new List<Agument> {new Agument(ref f), new Agument(ref m)};//参数列表
                    IntermediateCode iCode = new IntermediateCode(op, args, item, LexicalAnalysis.FilenameStack.Peek(),
                                                 LexicalAnalysis.Line);//中间代码
                    if(customProcess == null)//填入中间代码表
                    {
                        IntermediateCodeList.Add(iCode);
                        _numChain++;
                    }
                    else//填入自定义过程
                    {
                        customProcess.CodeBlock.Add(iCode);
                        _cusProNumChain++;
                    }
                }
                else
                {
                    item = f;
                }
            }
        }

        /// <summary>
        /// 项中间元素 k
        /// </summary>
        /// <returns></returns>
        static void ItemMidEle(CustomProcess customProcess, out String op, out Variable m)
        {
            op = "";
            m = null;
            if (LexicalAnalysis.Syn == 42 || LexicalAnalysis.Syn == 47)
            {
                op = LexicalAnalysis.Syn == 42 ? "*" : "/";
                LexicalAnalysis.Scanner();
                Factor(customProcess, out m); // 因子
                while (LexicalAnalysis.Syn == 42 || LexicalAnalysis.Syn == 47)
                {
                    Variable i;
                    String o = LexicalAnalysis.Syn == 42 ? "*" : "/";
                    LexicalAnalysis.Scanner();
                    Factor(customProcess, out i); 
                    if(m != null && i != null)
                    {
                        Variable res = new Variable(NewTemp());
                        List<Agument> args = new List<Agument>{new Agument(ref m), new Agument(ref i)};
                        IntermediateCode iCode = new IntermediateCode(o, args, res, LexicalAnalysis.FilenameStack.Peek(),
                                                                      LexicalAnalysis.Line);
                        if(customProcess == null)
                        {
                            IntermediateCodeList.Add(iCode);
                            _numChain++;
                        }
                        else
                        {
                            customProcess.CodeBlock.Add(iCode);
                            _cusProNumChain++;
                        }
                        m = res;
                    }
                }
            }
            else if (LexicalAnalysis.Token != "}" && LexicalAnalysis.Token != ")" && LexicalAnalysis.Token != "&" &&
                     LexicalAnalysis.Token != "|" && LexicalAnalysis.Token != ">" && LexicalAnalysis.Token != ">=" &&
                     LexicalAnalysis.Token != "<" && LexicalAnalysis.Token != "<=" && LexicalAnalysis.Token != "=" &&
                     LexicalAnalysis.Token != "!=" && LexicalAnalysis.Token != "\n" && LexicalAnalysis.Token != "+" &&
                     LexicalAnalysis.Token != "-" && LexicalAnalysis.Token != "[" && LexicalAnalysis.Syn != 132 &&
                     LexicalAnalysis.Token != "]" && LexicalAnalysis.Syn != 130 && LexicalAnalysis.Syn != 131 &&LexicalAnalysis.Syn != 40&&
                     LexicalAnalysis.Syn != 139 && LexicalAnalysis.Pointer < LexicalAnalysis.SourceCode.Peek().Length)
            //若不为k的Follow集
            {
                if (LexicalAnalysis.Syn == 10)
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line - 1,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                }
                else//一些错误处理
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                       LexicalAnalysis.FilenameStack.Peek()));

                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
        }

        /// <summary>
        /// 因子 L
        /// </summary>
        /// <returns></returns>
        static void Factor(CustomProcess customProcess, out Variable factor)
        {
            factor = null;
            if (LexicalAnalysis.Syn == 130) //数字
            {
                factor = new Variable(NewTemp(), LexicalAnalysis.Sum);
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 139) //引用变量
            {
                String v = VariRef();
                factor = v == "" ? null : new Variable(v);
            }
            else if (LexicalAnalysis.Syn == 40) //数值表达式
            {
                LexicalAnalysis.Scanner();
                NumExp(customProcess, out factor);
                if (LexicalAnalysis.Syn != 41)
                {
                    if (LexicalAnalysis.Syn == 10)
                    {
                        //一些错误处理
                        ConsoleHelper.StdErr(String.Format("语法错误：缺少\")\"\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    else //出错处理
                    {
                        ConsoleHelper.StdErr(String.Format("语法错误：缺少\")\"\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                }
                LexicalAnalysis.Scanner();
            }
            else
            {
                if (LexicalAnalysis.Syn == 10)
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line - 1,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                }
                else//出错处理
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                       LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
        }

        /// <summary>
        /// 变量引用 M
        /// </summary>
        /// <returns></returns>
        static String VariRef()
        {
            String ret = "";
            if (LexicalAnalysis.Syn == 139)//:
            {
                LexicalAnalysis.Scanner();
                if (LexicalAnalysis.Syn == 132)//标识符
                {
                    ret = LexicalAnalysis.Token;
                    LexicalAnalysis.Scanner();
                }
                else
                {
                    if (LexicalAnalysis.Syn == 10)//回车
                    {
                        //一些错误处理
                        ConsoleHelper.StdErr(String.Format("语法错误：引用变量格式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line - 1, LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    else
                    {
                        //一些错误处理
                        ConsoleHelper.StdErr(String.Format("语法错误：引用变量格式错误!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        LexicalAnalysis.Scanner();
                        Success = false;
                    }

                }
            }
            else
            {
                if (LexicalAnalysis.Syn == 10)
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：引用变量格式错误!\n\t错误位置：{1} line {0}\n",
                                                       LexicalAnalysis.Line - 1,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                }
                    //一些错误处理
                else
                {
                    ConsoleHelper.StdErr(String.Format("语法错误：引用变量格式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                       LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
            return ret;
        }

        /// <summary>
        /// 条件表达式 N
        /// </summary>
        /// <returns></returns>
        static void CondExp(CustomProcess customProcess, out Variable cond)
        {
            CondItem(customProcess, out cond); //条件项
            while (LexicalAnalysis.Syn == 141) //当为|时
            {
                Variable ci;
                LexicalAnalysis.Scanner();
                CondItem(customProcess, out ci);
                if (cond != null && ci != null)//添加至中间代码
                {
                    List<Agument> args = new List<Agument> { new Agument(ref cond), new Agument(ref ci) };
                    Variable res = new Variable(NewTemp());
                    IntermediateCode iCode = new IntermediateCode("|", args, res, LexicalAnalysis.FilenameStack.Peek(),
                                                                  LexicalAnalysis.Line);
                    if (customProcess == null)
                    {
                        IntermediateCodeList.Add(iCode);
                        _numChain++;
                    }
                    else
                    {
                        customProcess.CodeBlock.Add(iCode);
                        _cusProNumChain++;
                    }
                    cond = res;
                }
            
            }
        }

        /// <summary>
        /// 条件项 O
        /// </summary>
        /// <returns></returns>
        static void CondItem(CustomProcess customProcess, out Variable ci)
        {
            CondFactor(customProcess, out ci); //条件因子
            while (LexicalAnalysis.Syn == 142) //当为&时
            {
                Variable cf;
                LexicalAnalysis.Scanner();
                CondFactor(customProcess, out cf);
                if (ci != null && cf != null)//添加至中间代码
                {
                    List<Agument> args = new List<Agument> { new Agument(ref ci), new Agument(ref cf) };
                    Variable res = new Variable(NewTemp());
                    IntermediateCode iCode = new IntermediateCode("&", args, res, LexicalAnalysis.FilenameStack.Peek(),
                                                                  LexicalAnalysis.Line);
                    if (customProcess == null)
                    {
                        IntermediateCodeList.Add(iCode);
                        _numChain++;
                    }
                    else
                    {
                        customProcess.CodeBlock.Add(iCode);
                        _cusProNumChain++;
                    }
                    ci = res;
                }
            }
        }

        /// <summary>
        /// 条件因子 P
        /// </summary>
        /// <returns></returns>
        static void CondFactor(CustomProcess customProcess, out Variable cf)
        {
            cf = null;
            String not = "";
            if (LexicalAnalysis.Syn == 140) //!
            {
                not = "!";
                LexicalAnalysis.Scanner();
            }
            Variable cond = null; //关系因子（不包括!）
            int t1 = LexicalAnalysis.SourceCode.Peek().IndexOf(')', LexicalAnalysis.Pointer);
            int t2 = LexicalAnalysis.SourceCode.Peek().IndexOf('>', LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf('>', LexicalAnalysis.Pointer);
            int t3 = LexicalAnalysis.SourceCode.Peek().IndexOf(">=", LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf(">=", LexicalAnalysis.Pointer);
            int t4 = LexicalAnalysis.SourceCode.Peek().IndexOf("<=", LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf("<=", LexicalAnalysis.Pointer);
            int t5 = LexicalAnalysis.SourceCode.Peek().IndexOf('<', LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf('<', LexicalAnalysis.Pointer);
            int t6 = LexicalAnalysis.SourceCode.Peek().IndexOf("!=", LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf("!=", LexicalAnalysis.Pointer);
            int t7 = LexicalAnalysis.SourceCode.Peek().IndexOf('=', LexicalAnalysis.Pointer) == -1 ? t1 + 1 : LexicalAnalysis.SourceCode.Peek().IndexOf('=', LexicalAnalysis.Pointer);
            if (LexicalAnalysis.Syn == 130 || LexicalAnalysis.Syn == 139 || LexicalAnalysis.Syn == 131 ||
                LexicalAnalysis.Syn == 40  && t1 < t3 && t1 < t2 && t1 < t4 && t1 < t5 && t1 < t6 && t1 < t7)
                //条件因子的follow集
            {
                cond = new Variable(NewTemp());
                Variable p1;
                Parameter(customProcess, out p1); //参数分析
                String op = RelOpera();
                Variable p2;
                Parameter(customProcess, out p2);
                if (p1 != null && op != "" && p2 != null) //如果没有发生错误
                {
                    List<Agument> args = new List<Agument> {new Agument(ref p1), new Agument(ref p2)};
                    IntermediateCode iCodeCond = new IntermediateCode(op, args, cond,
                                                                      LexicalAnalysis.FilenameStack.Peek(),
                                                                      LexicalAnalysis.Line);
                    if (customProcess == null)
                    {
                        IntermediateCodeList.Add(iCodeCond);
                        _numChain++;
                    }
                    else
                    {
                        customProcess.CodeBlock.Add(iCodeCond);
                        _cusProNumChain++;
                    }
                }
            }
            else if (LexicalAnalysis.Syn == 40) //当为(时
            {
                LexicalAnalysis.Scanner();
                CondExp(customProcess, out cond);
                if (LexicalAnalysis.Syn == 41) //当为)时
                {
                    LexicalAnalysis.Scanner();
                }
                else
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：缺少\")\"!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                       LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
            else
            {
                //一些错误处理
                ConsoleHelper.StdErr(String.Format("语法错误：条件表达式错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
                LexicalAnalysis.Scanner();
            }
            if (not == "!" && cond != null)
            {
                cf = new Variable(NewTemp());
                List<Agument> arg = new List<Agument> {new Agument(ref cond)};
                IntermediateCode iCode = new IntermediateCode("!", arg, cf, LexicalAnalysis.FilenameStack.Peek(),
                                                              LexicalAnalysis.Line);
                if (customProcess == null)
                {
                    IntermediateCodeList.Add(iCode);
                    _numChain++;
                }
                else
                {
                    customProcess.CodeBlock.Add(iCode);
                    _cusProNumChain++;
                }
            }
            else if (cond != null)
            {
                cf = cond;
            }
        }

        /// <summary>
        /// 关系运算符 Q
        /// </summary>
        /// <returns></returns>
        static String RelOpera()
        {
            String ret = "";
            if (LexicalAnalysis.Syn == 133) //>
            {
                ret = ">";
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 134) //>=
            {
                ret = ">=";
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 135) //<
            {
                ret = "<";
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 136) //<=
            {
                ret = "<=";
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 137) //==
            {
                ret = "=";
                LexicalAnalysis.Scanner();
            }
            else if (LexicalAnalysis.Syn == 138) //!=
            {
                ret = "!=";
                LexicalAnalysis.Scanner();
            }
            else
            {
                //一些错误处理
                ConsoleHelper.StdErr(String.Format("语法错误：运算符错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
                LexicalAnalysis.Scanner();
            }
            return ret;
        }

        /// <summary>
        /// 引用文件 R
        /// </summary>
        /// <returns></returns>
        static void ReferenceFiles() //引用文件"include"
        {
            bool sign = false;
            if (LexicalAnalysis.Syn == 3)
            {
                LexicalAnalysis.Scanner();
                if (LexicalAnalysis.Syn == 131) //字符串
                {
                    if (FileHelper.IsFileExist(LexicalAnalysis.Token.Replace("\"", ""))) //判断文件是否存在
                    {
                        if (!LexicalAnalysis.FilenameStack.Contains(LexicalAnalysis.Token.Replace("\"", "")))
                        {
                            LexicalAnalysis.FilenameStack.Push(LexicalAnalysis.Token.Replace("\"", ""));
                            sign = true;
                        }
                        else
                        {
                            //一些错误处理
                            ConsoleHelper.StdErr(String.Format("语法错误：引用的文件循环调用!\n\t错误位置：{1} line {0}\n",
                                                               LexicalAnalysis.Line,
                                                               LexicalAnalysis.FilenameStack.Peek()));
                            Success = false;
                        }
                    }
                    else
                    {
                        //一些错误处理
                        ConsoleHelper.StdErr(String.Format("语法错误：引用的文件不存在!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                    }
                    LexicalAnalysis.Scanner();
                    if (LexicalAnalysis.Syn == 10||LexicalAnalysis.Pointer>=LexicalAnalysis.SourceCode.Peek().Count())
                    {
                        if (sign) //文件存在
                        {
                            LexicalAnalysis.SourceCode.Push(CompileHelper.PreCompile(FileHelper.ReadFile(LexicalAnalysis.FilenameStack.Peek())));
                            //保存现场
                            int currentPoint = LexicalAnalysis.Pointer;
                            int currentLine = LexicalAnalysis.Line;
                            LexicalAnalysis.Pointer = 0;
                            LexicalAnalysis.Line = 1;
                            Program();
                            //恢复现场
                            LexicalAnalysis.Pointer = currentPoint;
                            LexicalAnalysis.Line = currentLine;
                            LexicalAnalysis.SourceCode.Pop();
                            LexicalAnalysis.FilenameStack.Pop();
                        }
                        LexicalAnalysis.Scanner();
                    }
                    else
                    {
                        //一些错误处理
                        ConsoleHelper.StdErr(String.Format("语法错误：\"include\"语句需单独一行!\n\t错误位置：{1} line {0}\n",
                                                           LexicalAnalysis.Line,
                                                           LexicalAnalysis.FilenameStack.Peek()));
                        Success = false;
                        LexicalAnalysis.Scanner();
                    }
                }
                else
                {
                    //一些错误处理
                    ConsoleHelper.StdErr(String.Format("语法错误：文件名不合法!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                      LexicalAnalysis.FilenameStack.Peek()));
                    Success = false;
                    LexicalAnalysis.Scanner();
                }
            }
            else
            {
                //一些错误处理
                ConsoleHelper.StdErr(String.Format("语法错误：引用标识符错误!\n\t错误位置：{1} line {0}\n", LexicalAnalysis.Line,
                                                   LexicalAnalysis.FilenameStack.Peek()));
                Success = false;
                LexicalAnalysis.Scanner();
            }
        }

        //临时变量的编号
        private static int _tempNum = 0;
        //产生一个新的临时变量
        private static String NewTemp()
        {
            String ret = "#" + _tempNum;
            _tempNum++;
            return ret;
        }
        //控制语句的回填
        private static void BackPatch(int chain, int value, CustomProcess customProcess)
        {
            if(chain >= (customProcess ==null ? IntermediateCodeList.Count : customProcess.CodeBlock.Count))
            {
                return;//如果数组超出界限，说明有词法或者语法错误，应该直接返回
            }
            if(customProcess == null)
            {
                IntermediateCodeList[chain].Arguments.Add(new Agument(value));
                return;
            }
            customProcess.CodeBlock[chain].Arguments.Add(new Agument(value));
        }
    }
}
